package cn.hchaojie.camp.day02.exercise;

import java.util.Arrays;

/**
 * 写一个方法，传入一个整型数组，方法内部将数组元素倒置
 * 
 * @author hchaojie
 *
 */
public class ArrayShuffle {
	public static void main(String[] args) {
		int[] numbers1 = {1, 2, 3, 4};
		int[] numbers2 = {};
		int[] numbers3 = {5, 10, 21, 30, 40};
		
		shuffle(numbers1);
		System.out.println(Arrays.toString(numbers1));

		shuffle(numbers2);
		System.out.println(Arrays.toString(numbers2));

		shuffle(numbers3);
		System.out.println(Arrays.toString(numbers3));
	}

	public static void shuffle(int[] numbers) {
		for (int i = 0; i < numbers.length; i++) {
			int j = (int) (Math.random() * numbers.length);
			
			// 交换 i和j位置的元素
			int temp = numbers[i];
			numbers[i] = numbers[j];
			numbers[j] = temp;
		}
	}
}
